package mqtt;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerFilter;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.command.ConnectionInfo;

/**
 * Created by 10188 on 2018/4/9.
 */
public class AuthFilter extends BrokerFilter{

    public AuthFilter(Broker next) {
        super(next);
    }
    @Override
    public void addConnection(ConnectionContext context,
                              ConnectionInfo info) throws Exception {
        auth(info.getUserName(),info.getPassword());
        super.addConnection(context, info);
    }

    private void auth(String userName,String password)
    {

//为了演示方便写死了要验证的用户密码，实际实施时查db验证
        if(!"userName1".equals(userName) || !"password1".equals(password))
        {
            throw new SecurityException("Invalid  userName or password!");
        }
    }
}
